package net.alche.fa.biz.imp;

import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.function.Consumer;

import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;

import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.google.gson.reflect.TypeToken;

import java.util.HashMap;
import java.util.Iterator;

import net.alche.al.util.AC;
import net.alche.al.util.Far;
import net.alche.fa.biz.iface.FaHttpRequestResp;

@SuppressWarnings("all")
public class FaReq {
  FaHttpRequestResp resp = null;
  static HashMap<String, String> hm = new HashMap<>();
  String bodyStr = null;
  Map<String, String> bodyMpInfo = null;

  public FaReq(FaHttpRequestResp resp) {
    this.resp = resp;
  }

  public Object getSessionVal(String key) {
    return resp.getSessionVal(key);
  }

  public void setSessionVal(String key, Object val) {
    resp.setSessionVal(key, val);
  }
  // public String getParam(String key) {
  // return resp.getParam(key);
  // }

  // public String[] getParamValues(String key) {
  // return resp.getParamValues(key);
  // }
  boolean isPost() {
    return "post".equals(resp.getHttpType());
  }

  public String getStr(String name) {
    if (isPost())
      return bodyMap().get(name);
    String val = resp.getParam(name);
    if (val == null) {
      val = resp.getPathParam(name);
    }
    return val;
  }

  // non = 0
  public int getInt(String name) {
    String str = getStr(name);
    if (AC.empty(str)) {
      return 0;
    }
    return Integer.parseInt(str);
  }

  public void setContentType(String contextType) {
    resp.setContentType(contextType);
  }

  public void write(Object obj) {
    try {
      resp.write(obj);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  public void writeBytes(byte[] obj) {
    try {
      resp.writeBytes(obj);
    } catch (Exception ex) {
      ex.printStackTrace();
    }
  }

  // string
  public String getSessionStr(String key) {
    return AC.toStr(getSessionVal(key));
  }

  public String getBodyStr() {
    if (bodyStr == null) {
      bodyStr = resp.getBodyStr();
    }
    return bodyStr;
  }

  public Map<String, String> bodyMap() {
    if (bodyMpInfo != null) {
      return bodyMpInfo;
    }
    String json = getBodyStr();
    bodyMpInfo = new HashMap<>();
    try {
      JsonObject jsonObj = new JsonParser().parse(json).getAsJsonObject();
      Map<String, JsonElement> jsonMap = jsonObj.asMap();
      for (Entry<String, JsonElement> node : jsonMap.entrySet()) {
        String val = node.getValue().toString();
        if (val.startsWith("\"") && val.endsWith("\"")) {
          val = val.substring(1, val.length() - 1);
        }
        val = StringEscapeUtils.unescapeJson(val);
        bodyMpInfo.put(node.getKey(), val);
      }
    } catch (Exception e) {
      e.printStackTrace();
    }
    return bodyMpInfo;
  }

  public Map<String, Object> bindMap() {
    return Far.tl.fromJsonMap(getBodyStr());
  }

  public <T> T bind(Class cls) {
    return (T) Far.tl.parseJson(getBodyStr(), cls);
  }

  public <T> List<T> bindLs(Class cls) {
    return (List<T>) Far.tl.parseJsonList(getBodyStr(), cls);
  }

  public <T> T bind(String name, Class cls) {
    return (T) Far.tl.parseJson(bodyMap().get(name), cls);
  }

  public <T> List<T> bindLs(String name, Class cls) {
    return (List<T>) Far.tl.parseJsonList(bodyMap().get(name), cls);
  }

}
